package Reports;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Retail.RetailProduct;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;


public class SaleReportDaily extends HttpServlet{

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req, resp);
	}
	
	
	protected void doProcess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		List<transaction> item = new ArrayList<transaction>();
		String day = null;
	
		if ((req.getParameter("SReportDaily") == null)||req.getParameter("SReportDaily")=="")
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			 day = sdf.format(new Date());
			 System.out.print("hello");
		}
		else
		{
			 day = req.getParameter("SReportDaily");	
			 System.out.print("hello1");
		}
		System.out.print(req.getParameter("SReportDaily"));
		System.out.print(day);
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail",
					"sa", "");
			PreparedStatement ps = con.prepareStatement("select * from orders where cast(TRANSACTION_TIME as varchar(10))=(?)");
			ps.setString(1, day);
			ResultSet rs = ps.executeQuery();
			
		   
			while (rs.next()) {
				
				transaction Tran = new transaction();
		
				Tran.setTransactionID(rs.getString("ORDERID"));
				Tran.setProductID(rs.getString("PRODUCTID"));
				Tran.setUser(rs.getString("OPERATOR"));
				Tran.setMember(rs.getString("OPERATOR"));
				Tran.setQuantitly(rs.getInt("QUANTITY"));
				Tran.setTransactionAmount(rs.getBigDecimal("SOLD_PRICE"));
				Tran.setPaymentMethod(rs.getString("PAYMENT_WAY"));
				
				
				item.add(Tran);
				System.out.print(item);
			} 
			
			ps.close();
			con.close();
			
			
			   if (!(item.isEmpty())){
		        JasperPrint jasperPrint = null;  
		        
		        try {  
		        	JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream("SalesReport.jrxml")); 
		            jasperPrint = JasperFillManager.fillReport(jasperReport,null,  
		                    new JRBeanCollectionDataSource(item));  
		        } catch (JRException e) {  
		            e.printStackTrace();  
		            resp.sendRedirect("Report/ReportEmpty.jsp");
		        }  

		        
		        resp.setContentType("application/pdf");
		        OutputStream outStream = resp.getOutputStream();
		        try {
					JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
				} catch (JRException e) {
					// TODO Auto-generated catch block
					resp.sendRedirect("Report/ReportEmpty.jsp");
				}
				
			   }
			   else{
				   System.out.print("empty");
				    resp.sendRedirect("Report/ReportEmpty.jsp");
			   }
			
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
		
		
		
		
		
		
	}
	
	
